package org.firebug.spring.boot.ssj.web.controller;

import org.firebug.common.bean.api.Meta;
import org.firebug.common.bean.api.Pagination;
import org.firebug.common.bean.api.Response;
import org.firebug.spring.boot.ssj.repository.qo.UserQO;
import org.firebug.spring.boot.ssj.service.UserService;
import org.firebug.spring.boot.ssj.web.vo.UserVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response save(@RequestBody UserVO vo) {
        userService.save(vo);
        return Response._200;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Response delete(@PathVariable long id) {
        userService.deletePhysicalById(id);
        return Response._200;
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Response update(@Validated @RequestBody UserVO vo) {
        userService.updateById(vo);
        return Response._200;
    }

    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET)
    public Response<List<UserVO>> query(@PathVariable long id) {
        return Response.newInstance(Meta._200, userService.selectById(id));
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Response<List<UserVO>> list(UserQO qo, Pagination pagination) {
        List<UserVO> vos = userService.selectByQO(qo, pagination);
        return Response.newInstance(Meta._200, vos, pagination);
    }
}

